package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.market.Brand;
import com.cskaoyan.bean.market.BrandCreateBo;
import com.cskaoyan.bean.market.BrandCreateVo;
import com.cskaoyan.bean.market.BrandList;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.service.BrandService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *  商场管理模块 品牌制造商单元
 *  赵维虎
 */

@RestController
@RequestMapping("admin/brand")
public class BrandController {

    @Autowired
    BrandService brandService;

    //admin/brand/list?page=1&limit=20&id=111&name=2222&sort=add_time&order=desc
    @GetMapping("list")
    public BaseRespVo list(BaseParam baseParam, Integer id, String name) {
        BrandList brandList = brandService.list(baseParam,id,name);
        return BaseRespVo.ok(brandList);
    }

    @RequiresPermissions(value ={"*"},logical = Logical.OR)
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Brand brand){
        brandService.delete(brand);
        return BaseRespVo.ok();
    }

    @RequiresPermissions(value ={"*","admin:brand:update"},logical = Logical.OR)
    @PostMapping("update")
    public BaseRespVo update(@RequestBody Brand brand) {
        Brand brand1 = brandService.update(brand);
        return BaseRespVo.ok(brand1);
    }


    @RequiresPermissions(value ={"*","admin:brand:create"},logical = Logical.OR)
    @PostMapping("create")
    public BaseRespVo create(@RequestBody BrandCreateBo brandCreateBo) {
        BrandCreateVo brandCreateVo = brandService.create(brandCreateBo);
        if(brandCreateVo == null){
            return BaseRespVo.fail("已有该品牌商");
        }else if ("a".equals(brandCreateVo.getDesc())){
            return BaseRespVo.fail("请上传品牌商图片或者上输入底价");
        }
        return BaseRespVo.ok(brandCreateVo);
    }
}
